package com.lib.framework.widget.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import com.lib.framework.R
import com.lib.framework.widget.WidgetUtil
import java.lang.Exception

/**
 * 默认对话框
 *
 * @author keanbin
 */
abstract class BaseDialog(context: Context, themeResId: Int) : Dialog(context, themeResId) {

    protected val mTag = this.javaClass.canonicalName ?: "BaseDialog"

    protected val mContext = context

    /**
     * Loading 对话框
     */
    private var mLoadingDialog: Dialog? = null

    constructor(context: Context) : this(context, R.style.defaultDialogTheme)

    /**
     * 显示
     *
     * @param relativeTo
     * 显示的位置相对于该View
     * @param locationInParent
     * 与相对于的View之间的x，y差值。locationInParent[0]为x，locationInParent[1]为y。
     */
    fun showAtLocation(relativeTo: View?, locationInParent: IntArray) {
        val relativeToLocation = IntArray(2)
        relativeToLocation[0] = 0
        relativeToLocation[1] = 0

        if (relativeTo != null) {
            relativeTo.getLocationInWindow(relativeToLocation)

            if (mContext is Activity) {
                // 减去状态栏的高度
//                if (AppUtil.isHasStatusBar(mContext))
//                    relativeToLocation[1] -= QMUIStatusBarHelper.getStatusbarHeight(mContext)
            }
        }

        window?.let {
            it.setGravity(Gravity.LEFT or Gravity.TOP)

            val params = it.attributes
            params.x = relativeToLocation[0] + locationInParent[0] // 设置x坐标
            params.y = relativeToLocation[1] + locationInParent[1] // 设置y坐标
            it.attributes = params

            show()
        }
    }

    /**
     * 设置宽度
     *
     * @param width
     * 如果为-1，相当于match_parent；如果为-2，相当于wrap_content。
     */
    fun setWidth(width: Int) {
        window?.let {
            val params = it.attributes
            params.width = width
            it.attributes = params
        }
    }

    /**
     * 设置高度
     *
     * @param height
     * 如果为-1，相当于match_parent；如果为-2，相当于wrap_content。
     */
    fun setHeight(height: Int) {
        window?.let {
            val params = it.attributes
            params.height = height
            it.attributes = params
        }
    }

    /**
     * 设置对话框的位置(使用父类的show（）方法才有效)
     *
     * @param gravity
     * 比如：Gravity.BOTTOM 显示在底部
     */
    fun setGravity(gravity: Int) {
        window?.let {
            it.setGravity(gravity)
        }
    }

    /**
     * 显示 提示
     */
    protected fun showPrompt(strResid: Int) {
        showPrompt(mContext.getString(strResid))
    }

    /**
     * 显示 提示
     */
    protected fun showPrompt(str: String?) {
        WidgetUtil.showPromptDialog(mContext, str ?: "")
    }

    /**
     * 显示 Loading 对话框
     *
     * @param isCancelable  用户是否可以主动取消对话框
     */
    protected fun showLoadingDialog(isCancelable: Boolean = false, text: String? = null) {
        closeLoadingDialog()

        mLoadingDialog = WidgetUtil.createLoadingDialog(context, text)
        mLoadingDialog?.setCancelable(isCancelable)
        //mLoadingDialog?.setCanceledOnTouchOutside(isCancelable)
        mLoadingDialog?.show()
    }

    /**
     * 关闭 Loading 对话框
     */
    protected fun closeLoadingDialog() {
        mLoadingDialog?.cancel()
        mLoadingDialog = null
    }

    override fun dismiss() {
        closeLoadingDialog()
        super.dismiss()
    }

    override fun show() {
        try {
            super.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}